package service

import (
	"MyBlogv2/common/HttpCode"
	"MyBlogv2/common/define"
	"MyBlogv2/common/models"
	"MyBlogv2/common/redisUtil"
	"MyBlogv2/common/response"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
)

func Like(c *gin.Context) {
	typeOfLike, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")
	var dbCount int64
	models.Db.Model(new(models.Like)).Where("user_id = ? and type = ? and type_id = ?", userId, typeOfLike, typeId).Count(&dbCount)
	if dbCount > 0 {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	var like models.Like
	like.UserId = userId.(int)
	like.Type = typeOfLike
	like.TypeId = typeId
	models.Db.Model(new(models.Like)).Create(&like)
	if typeOfLike == define.LIKE_TYPE_ARTICLE {
		redisUtil.AddLikeCount(typeId)
		
		go func() {
			if err := SyncLikeAction(userId.(int), typeId); err != nil {
				fmt.Printf("同步点赞到Neo4j失败: %v\n", err)
			}
		}()
	}
	response.ResponseOK(c, nil)
}

func CancelLike(c *gin.Context) {
	typeOfLike, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")
	models.Db.Model(new(models.Like)).Where("user_id = ? and type = ? and type_id = ?", userId, typeOfLike, typeId).Delete(new(models.Like))
	if typeOfLike == define.LIKE_TYPE_ARTICLE {
		redisUtil.DelLikeCount(typeId)
		
		go func() {
			if err := SyncCancelLikeAction(userId.(int), typeId); err != nil {
				fmt.Printf("同步取消点赞到Neo4j失败: %v\n", err)
			}
		}()
	}
	response.ResponseOK(c, nil)
}

func IsLike(c *gin.Context) {
	typeOfLike, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")

	fmt.Println(typeOfLike, typeId, userId)
	if typeOfLike == define.LIKE_TYPE_ARTICLE {
		fmt.Println("typeOfLike", typeOfLike)
		var like []models.LikeVo
		var dbCount int64
		models.Db.Model(new(models.LikeVo)).Where("user_id = ? and type = ? and type_id = ?", userId, typeOfLike, typeId).Count(&dbCount).Find(&like)
		for i, v := range like {
			like[i].IdStr = strconv.Itoa(v.Id)
		}
		if dbCount > 0 {
			response.ResponseOK(c, like)
		} else {
			response.ResponseError(c, HttpCode.FAILURE)
		}
		return
	}
	if typeOfLike == define.LIKE_TYPE_COMMENT || typeOfLike == define.LIKE_TYPE_LEAVE_WORD {
		fmt.Println("typeOfLike", typeOfLike)
		var like []models.LikeVo
		var dbCount int64
		db := models.Db.Model(new(models.LikeVo))
		if typeOfLike == define.LIKE_TYPE_LEAVE_WORD {
			db = db.Where("type_id = ?", typeId)
		}
		db.Where("user_id = ? and type = ?", userId, typeOfLike).Count(&dbCount).Find(&like)
		for i, v := range like {
			like[i].IdStr = strconv.Itoa(v.Id)
		}
		if dbCount > 0 {
			response.ResponseOK(c, like)
		} else {
			response.ResponseError(c, HttpCode.FAILURE)
		}
		return

	}
	response.ResponseError(c, HttpCode.FAILURE)
}

func GetLikeCount(c *gin.Context) int {
	typeOfComment, _ := strconv.Atoi(c.Param("type"))
	typeId, _ := strconv.Atoi(c.Param("id"))
	var count int64
	models.Db.Model(new(models.Like)).Where("type = ? and type_id = ?", typeOfComment, typeId).Count(&count)
	return int(count)

}
